﻿/******************************************************************************
    myBoard is a port of MacOS X dashboard application for windows.
    Copyright (C) 2007,2008  by Yann ALET
    Email : yann.alet@gmail.com

    myBoard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    myBoard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
****************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using myBoard.Framework;
using System.Windows.Threading;
using System.Diagnostics;
using System.Reflection;

namespace CPUMeter
{
    /// <summary>
    /// Interaction logic for EmptyMagnet.xaml
    /// </summary>
    public partial class Magnet : UserControl , IMagnet
    {
        PerformanceCounter _cpuCounter;
        PerformanceCounter _ramCounter;
        GradientStop _originalGaugeGradient1;
        GradientStop _originalGaugeGradient2;
        DispatcherTimer _timer;

        /// <summary>
        /// This is the magnet constructor, add all initialization code here.
        /// </summary>        
        public Magnet()
        {
            InitializeComponent();
            _originalGaugeGradient1 = _gaugeGradient1;
            _originalGaugeGradient2 = _gaugeGradient2;
            _cpuCounter = new PerformanceCounter();
            _cpuCounter.CategoryName = "Processor";
            _cpuCounter.CounterName = "% Processor Time";
            _cpuCounter.InstanceName = "_Total";
            _ramCounter = new PerformanceCounter("Memory", "Available MBytes"); 
            _timer = new DispatcherTimer();
            _timer.Interval = new TimeSpan(0, 0, 1);
            _timer.Tick += new EventHandler(timer_Tick);
            _timer.Start();
            
        }

        void timer_Tick(object sender, EventArgs e)
        {
            int cpu = Convert.ToInt16(_cpuCounter.NextValue());
            _cpuUsage.Content = cpu.ToString().PadLeft(2, '0');
            _ram.Content = _ramCounter.NextValue();
            double red = Math.Min((255 * (cpu * 0.01))*1.1,255);
            int green = 255 - Convert.ToInt16(red);
            //Debug.WriteLine(string.Format("cpu:{3}, r:{0} , g:{1}, b:{2}", red, green, _originalGaugeGradient1.Color.B, cpu));
            _gaugeGradient1.Color = Color.FromArgb(255,Convert.ToByte(red * 0.6), Convert.ToByte(green*0.6), _originalGaugeGradient1.Color.B);
            _gaugeGradient2.Color = Color.FromArgb(255,Convert.ToByte(red), (byte)green, _originalGaugeGradient1.Color.B);
        }
        

        #region IMagnet Members

        #region Magnets events for myBoard

        /// <summary>
        /// This event should be raised when you want to hide myBoard
        /// </summary>
        public event EventHandler CloseMyBoardRequested;

        /// <summary>
        /// This event should be raised when you want to flip your magnet
        /// </summary>
        public event OnFlipRequested FlipRequested;

        #endregion

        #region Magnets methods

        /// <summary>
        /// This method should return the magnet's back side container (what is going to be displayed when you flip the magnet)
        /// You can return null if your magnet doesn't have any backside.
        /// </summary>
        /// <returns></returns>
        public Panel GetBackSide()
        {
            return null;
        }

        /// <summary>
        /// When you flip a magnet, myBoard need to add a special 3D control in the magnet to create the flip animation.
        /// This method returns the container myBoard should use to put the 3D control for flip animation.
        /// You can return null if your magnet doesn't have any backside.
        /// </summary>
        /// <returns></returns>
        public Panel GetFlipZone()
        {
            return null;
        }

        /// <summary>
        /// This method returns the magnet's front side container (what is being displayed when you first drop the magnet.
        /// </summary>
        /// <returns></returns>

        public Panel GetFrontSide()
        {
            return _front;
        }

        public BitmapImage Icon
        {
            get
            {
                return ResourcesAccess.GetBitmapImageFromResource(Assembly.GetExecutingAssembly(), "CPUMeter.Images.Icon.png");
            }
        }

        BitmapImage _thumbnail;

        public BitmapImage Thumbnail
        {
            get
            {
                if (_thumbnail == null)
                {
                    _thumbnail = Tools.GetBitmapImageFromFrameworkElement(this);
                }
                return _thumbnail;
            }
        }

        /// <summary>
        /// This is where you define your magnet's name. It is used in the magnet explorer just under the magnet logo.
        /// </summary>
        public string MagnetName
        {
            get { return "CPU Meter"; }
        }

        /// <summary>
        /// This method is regulary called by myBoard to saved the magnet state.  
        /// Note : This method will most likely be renamed in next version of myBoard.
        /// </summary>
        public void Terminate()
        {
            _timer.Stop();
            _timer = null;
        }

        #endregion

        #endregion
    }
}